package com.xmall.service.impl;

import com.github.pagehelper.PageInfo;
import com.xmall.bean.table.Brand;
import com.xmall.bean.table.BrandExample;
import com.xmall.mapper.BrandMapper;
import com.xmall.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public Map<String,Object> showBrandList(Integer page, Integer size) {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("brandList",brands);
        map.put("totalPages",total);
        return map;
    }

    @Override
    public Brand showBrandDetailById(Integer id) {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        Brand brand = brands.get(0);
        return brand;
    }
}
